package org.example.j2024_11_4;

import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RequestMapping("/cookie")
@RestController
public class CookieController {
     @RequestMapping("/c1")
    public String c1(HttpServletRequest request){
         Cookie[] cookies = request.getCookies();
         if(cookies == null) return "没有cookie";
         Arrays.stream(cookies).forEach(x-> System.out.println("name:"+x.getName()+"value:"+x.getValue()));
         return "cookie";
     }

     @RequestMapping("/c2")
    public String c2(@CookieValue("a1") Cookie cookie ){
         return "cookie:"+cookie.getName()+"value:"+cookie.getValue() ;
     }

     @RequestMapping("/c3")
    public String c3(@CookieValue(value = "a1", required = false)Cookie c1 ,
                     @CookieValue(value = "b1" ,required = false ) Cookie c2){
         if(c1 != null)
         System.out.println("a1:"+c1.getValue());
         if(c2!=null)
         System.out.println("b1"+c2.getValue());

         return "cookie";
     }
}
